package org.testis.testdriver;

import org.testis.testdriver.exceptions.NoSuchValidateStrategyException;
import org.testis.testdriver.validation.TestPartValidateStrategy;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;


/**
 *
 * @author barisergun
 */
public class TestPartValidateContext
{

    private TestPartValidateStrategy testPartRunSrategy = null;


    public TestPartValidateContext(TestPart testPartToRun)
    {

        try {

            Class<?> cls = Class.forName("org.testis.testdriver.validation.TestPartValidate" +
                                         testPartToRun.getContainerType().
                                         toString() + "Strategy");
            Constructor[] constr = cls.getDeclaredConstructors();
            Object[] params = new Object[]{testPartToRun};
            for (int i = 0; i < constr.length; i++) {
                Class[] constParams = constr[i].getParameterTypes();
                if (constParams.length == 1) {
                    testPartRunSrategy = (TestPartValidateStrategy) constr[i].
                        newInstance(
                        params);

                }
            }

        } catch (InstantiationException ex) {
        } catch (IllegalAccessException ex) {
        } catch (IllegalArgumentException ex) {
        } catch (ClassNotFoundException ex) {
        } catch (InvocationTargetException ex) {
        } finally {
            if (testPartRunSrategy == null) {
                throw new NoSuchValidateStrategyException(
                    "TestPartValidate" +
                    testPartToRun.getContainerType().
                    toString() + "Strategy does not exist");
            }
        }



    }


    public boolean validatePartResult(TestIterator testIterInRun,
                                      AtofEnumaration.DebuggingMode mode,
                                      boolean partResult)
    {
         return testPartRunSrategy.validatePartResult(testIterInRun,
                                                     mode,
                                                     partResult);
    }

    public boolean validateFinalPartResult(TestIterator testIterInRun,
                                      AtofEnumaration.DebuggingMode mode,
                                      boolean partResult)
    {
        return testPartRunSrategy.validateFinalPartResult(testIterInRun,
                                                     mode,
                                                     partResult);
    }
}
